﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.minigames {
    import Classes.*;
    import Modules.bandits.*;
    import Classes.actions.*;
    import com.zynga.skelly.util.*;

    public class GremlinsMiniGame extends MiniGame {

        public static const GREMLINS_MINI_GAME:String = "GremlinsMiniGame";
        public static const NUM_GREMLINS:int = 10;

        public var gremArray:Array;

        public static function canStartMiniGame():Boolean{
            return (Global.world.citySim.roadManager.hasWalkableRoad());
        }

        override protected function get name():String{
            return (GREMLINS_MINI_GAME);
        }
        override protected function get timeLimit():int{
            return (30000);
        }
        override public function init():void{
            super.init();
            Global.hud.updateMiniGame();
            this.gremArray = new Array();
            var _local1:int;
            while (_local1 < NUM_GREMLINS) {
                this.spawnGremlin();
                _local1++;
            };
        }
        override public function update(_arg1:Number):void{
            super.update(_arg1);
        }
        public function updateScore():void{
            m_score++;
            Global.hud.updateMiniGame();
            if (m_score >= NUM_GREMLINS){
                complete();
            };
        }
        public function spawnGremlin():void{
            var bandit:* = null;
            var road:* = Global.world.citySim.roadManager.findRandomRoad();
            bandit = Global.world.citySim.npcManager.createWalkerByNameAtPosition("energy_bandit", road.getHotspot(), true);
            bandit.actionSelection = new BanditActionSelection(bandit);
            this.gremArray.push(bandit);
            bandit.playActionCallback = Curry.curry(function (_arg1:NPC):void{
                var npc:* = _arg1;
                if (((m_isActive) && ((npc.alpha == 1)))){
                    updateScore();
                    npc.alpha = 0.99;
                };
                npc.getStateMachine().removeAllStates();
                bandit.getStateMachine().addActions(new ActionTween(npc, ActionTween.TO, 0.5, {alpha:0}), new ActionFn(npc, Curry.curry(function (_arg1:NPC):void{
                    Global.world.citySim.npcManager.removeWalker(_arg1);
                }, npc)));
            }, bandit);
        }
        override public function onEnd():void{
            var npc:* = null;
            for each (npc in this.gremArray) {
                npc.getStateMachine().removeAllStates();
                npc.getStateMachine().addActions(new ActionTween(npc, ActionTween.TO, 0.5, {alpha:0}), new ActionFn(npc, Curry.curry(function (_arg1:NPC):void{
                    Global.world.citySim.npcManager.removeWalker(_arg1);
                }, npc)));
            };
            super.onEnd();
            Global.hud.updateMiniGame();
        }

    }
}//package Modules.minigames 
